﻿''' <summary>
''' Tracks and provides access to the mouse and keyboard input for the game.
''' </summary>
''' <remarks></remarks>
Public Class GameInput

    Protected Friend _ActiveKeyboardState As New KeyboardStateInfo
    Protected Friend _ActiveMouseState As New MouseStateInfo

    Protected _CurrentKeyboardState As KeyboardStateInfo
    ''' <summary>
    ''' Provides access to the state of the keyboard during this frame.
    ''' </summary>
    ''' <value></value>
    ''' <returns>The state of the keyboard during this frame.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property CurrentKeyboardState As KeyboardStateInfo
        Get
            Return _CurrentKeyboardState
        End Get
    End Property

    Protected _PreviousKeyboardState As KeyboardStateInfo
    ''' <summary>
    ''' Provides access to the state of the keyboard during the previous frame.
    ''' </summary>
    ''' <value></value>
    ''' <returns>The state of the keyboard during the previous frame.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property PreviousKeyboardState As KeyboardStateInfo
        Get
            Return _PreviousKeyboardState
        End Get
    End Property

    Protected _CurrentMouseState As MouseStateInfo
    ''' <summary>
    ''' Provides access to the state of the mouse during this frame.
    ''' </summary>
    ''' <value></value>
    ''' <returns>The state of the mouse during this frame.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property CurrentMouseState As MouseStateInfo
        Get
            Return _CurrentMouseState
        End Get
    End Property

    Protected _PreviousMouseState As MouseStateInfo
    ''' <summary>
    ''' Provides access to the state of the mouse during the previous frame.
    ''' </summary>
    ''' <value></value>
    ''' <returns>The state of the mouse during the previous frame.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property PreviousMouseState As MouseStateInfo
        Get
            Return _PreviousMouseState
        End Get
    End Property

    Protected Friend Overridable Sub OnUpdate(ByVal e As GameEngineEventArgs)
        _PreviousKeyboardState = _CurrentKeyboardState
        _CurrentKeyboardState = _ActiveKeyboardState
        _PreviousMouseState = _CurrentMouseState
        _CurrentMouseState = _ActiveMouseState
    End Sub

    ''' <summary>
    ''' Determines if the mouse position has changed since the last frame.
    ''' </summary>
    ''' <returns>True if the mouse position has changed, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Function DidMouseMove() As Boolean
        Return Not _CurrentMouseState.MouseLocation = _PreviousMouseState.MouseLocation
    End Function

    ''' <summary>
    ''' Gets the current mouse position.
    ''' </summary>
    ''' <returns>The current mouse position.</returns>
    ''' <remarks></remarks>
    Public Function GetMouseLocation() As Point
        Return _CurrentMouseState.MouseLocation
    End Function

    ''' <summary>
    ''' Determines if the specified mouse button is currently pressed.
    ''' </summary>
    ''' <param name="button">The mouse button to check.</param>
    ''' <returns>True if the specified button is down, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Function IsMouseButtonDown(ByVal button As MouseButtons) As Boolean
        Return _CurrentMouseState(button)
    End Function

    ''' <summary>
    ''' Determines if the specified mouse button is currently released.
    ''' </summary>
    ''' <param name="button">The mouse button to check.</param>
    ''' <returns>True if the specified button is up, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Function IsMouseButtonUp(ByVal button As MouseButtons) As Boolean
        Return Not _CurrentMouseState(button)
    End Function

    ''' <summary>
    ''' Determines if the specified mouse button was pressed and released.
    ''' </summary>
    ''' <param name="button">The mouse button to check.</param>
    ''' <returns>True if the specified button was pressed, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Function WasMouseButtonPressed(ByVal button As MouseButtons) As Boolean
        Return _PreviousMouseState(button) AndAlso Not _CurrentMouseState(button)
    End Function

    ''' <summary>
    ''' Determines if the specified key is currently pressed.
    ''' </summary>
    ''' <param name="key">The key to check.</param>
    ''' <returns>True if the specified key is down, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Function IsKeyDown(ByVal key As Keys) As Boolean
        Return _CurrentKeyboardState(key)
    End Function

    ''' <summary>
    ''' Determines if the specified key is currently released.
    ''' </summary>
    ''' <param name="key">The key to check.</param>
    ''' <returns>True if the specified key is up, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Function IsKeyUp(ByVal key As Keys) As Boolean
        Return Not _CurrentKeyboardState(key)
    End Function

    ''' <summary>
    ''' Determines if the specified key was pressed and released.
    ''' </summary>
    ''' <param name="key">The key to check.</param>
    ''' <returns>True if the specified key was pressed, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Function WasKeyPressed(ByVal key As Keys) As Boolean
        Return _PreviousKeyboardState(key) AndAlso Not _CurrentKeyboardState(key)
    End Function
End Class
